<template>
  <div>
    <!-- 展示sku -->
    <el-table style="width: 100%" border :data="skuList">
      <el-table-column label="序号" width="80" type="index" align="center">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column>
      <el-table-column label="默认图片" width="110" align="center">
        <template slot-scope="{ row }">
          <img :src="row.skuDefaultImg" style="width: 82px; height: 82px" />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量(kg)" width="80" align="center">
      </el-table-column>
      <el-table-column prop="price" label="价格(元)" width="80" align="center">
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{ row }">
          <el-button
            icon="el-icon-bottom"
            size="small"
            title="下架"
            v-if="row.isSale == 0"
            @click="cancelSale(row)"
          ></el-button>
          <el-button
            type="success"
            icon="el-icon-top"
            size="small"
            title="上架"
            v-else
            @click="onSale(row)"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            title="编辑"
          ></el-button>
          <el-button
            type="info"
            icon="el-icon-info"
            size="small"
            title="查看"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete-solid"
            size="small"
            title="删除"
            @click="deleteSku(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 查看sku信息 -->
    <el-drawer :visible.sync="show" size="40%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}.00元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="450px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" style="width:450px;height:450px">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
    <!-- 分页 -->
    <el-pagination
      style="text-align: center"
      :current-page="page"
      :page-sizes="[5, 10, 15]"
      :page-size="limit"
      layout="prev, pager, next,->,sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 10,
      total: 0,
      skuList: [],
      show: false, //抽屉显示隐藏
      skuInfo: {},
    };
  },
  mounted() {
    this.getSkuList();
  },
  methods: {
    //获取sku列表
    async getSkuList() {
      let result = await this.$API.sku.reqSkuList(this.page, this.limit);
      if (result.code == 200) {
        this.total = result.data.total;
        this.skuList = result.data.records;
      }
    },
    //改变当前页
    handleCurrentChange(page) {
      this.page = page;
      this.getSkuList();
    },
    //改变一页的数据个数
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSkuList();
    },
    //下架
    async cancelSale(sku) {
      let result = await this.$API.sku.reqCancelSale(sku.id);
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: "下架成功",
        });
        sku.isSale = 1;
      }
    },
    //上架
    async onSale(sku) {
      let result = await this.$API.sku.reqOnSale(sku.id);
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: "上架成功",
        });
        sku.isSale = 0;
      }
    },
    //查看sku
    async getSkuInfo(sku) {
      let result = await this.$API.sku.reqSkuById(sku.id);
      if (result.code == 200) {
        this.skuInfo = result.data;
        this.show = true;
      }
    },
    async deleteSku(sku){
      let result=await this.$API.sku.reqDeleteSku(sku.id)
      if(result.code==200){
        this.$message('删除成功')
        this.getSkuList()
      }
    }
  },
};
</script>

<style scoped>
.el-row .el-col-5 {
  text-align: right;
  font-size: 18px;
}
.el-col {
  margin: 10px 10px;
}
</style>